package com.xc.content.service;

import com.xc.content.bo.CourseCategoryBO;
import com.xc.content.constant.ExceptionCode;
import com.xc.content.convert.CourseCategoryServiceConverter;
import com.xc.content.entity.CourseCategory;
import com.xc.content.exception.DatabaseException;
import com.xc.content.exception.SystemException;
import com.xc.content.mapper.CourseCategoryMapper;
import com.xc.content.utils.ExceptionUtil;
import com.xc.content.utils.SnowflakeUtil;
import com.xc.content.utils.TreeNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;


import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * 课程分类(CourseCategory)表服务接口
 *
 * @author makejava
 * @since 2025-01-03 15:55:59
 */
@Service("courseCategoryService")
@Slf4j
public class CourseCategoryService {

    @Resource
    private CourseCategoryMapper courseCategoryMapper;


    /**
     * 新增分类接口
     *
     * @param courseCategoryBO
     * @return
     */
    public Boolean addCourseCategory(CourseCategoryBO courseCategoryBO) {
        String name = courseCategoryBO.getName();
        CourseCategory data = courseCategoryMapper.queryByName(name);
        if (!ObjectUtils.isEmpty(data)) {
            log.error("该分类名称已存在,请更换新的分类名称");
            throw new DatabaseException(500,"分类名称为:" + courseCategoryBO.getName() + "已存在,请更换名称提交!");
        }
        try {
            CourseCategory courseCategory = CourseCategoryServiceConverter.INSTANCE.convertBOToEntity(courseCategoryBO);
            String id = SnowflakeUtil.nextId();
            courseCategory.setId(id);
            Integer isAdd = courseCategoryMapper.addCourseCategory(courseCategory);
            return isAdd > 0;
        } catch (SystemException e) {
            throw new SystemException(ExceptionCode.FIVE_HUNDRED_CODE, "接口内部异常", e);
        }
    }

    /**
     * 树型查询分类接口
     * @return
     */
    public List<TreeNode> getCourseCategoryTree() {
        List<CourseCategory> courseCategories = courseCategoryMapper.queryList();

        List<TreeNode> courseCategoryVos = courseCategories.stream()
                .map(courseCategory -> {
                    TreeNode courseCategoryVo = new TreeNode();
                    BeanUtils.copyProperties(courseCategory, courseCategoryVo);
                    return courseCategoryVo;
                })
                .collect(Collectors.toList());

        List<TreeNode> treeNodeList = courseCategoryVos.stream()
                .filter(courseCategoriesVo -> courseCategoriesVo.getParentId().equals("0"))
                .peek((menu) -> menu.setChildren(getChildrenData(menu, courseCategoryVos)
                ))
                .collect(Collectors.toList());
        return treeNodeList;
    }

    /**
     * 获取孩子（下级目录）的方法，递归实现
     * @param menu 当前节点
     * @param courseCategoryVos 所有节点列表
     * @return 当前节点的子节点列表
     */
    public List<TreeNode> getChildrenData(TreeNode menu, List<TreeNode> courseCategoryVos) {
        if(menu == null){
            return new ArrayList<>();
        }
        List<TreeNode> treeNodes = courseCategoryVos.stream()
                .filter(courseCategoryVo -> courseCategoryVo.getParentId().equals(menu.getId()))
                .peek(courseCategoryVo -> courseCategoryVo.setChildren(getChildrenData(courseCategoryVo, courseCategoryVos)))
                .collect(Collectors.toList());
        return treeNodes;
    }


}
